beginoutdoorscript;

variables;
short choice, safety, pc_counter;

body;

beginstate 0; //INIT_STATE
break;

beginstate 1; //EXIT_STATE
break;

beginstate 2; //START_STATE

	//fixing up the camps outside of Copperpeak
	if((get_current_tick() % 5000 >= 3750) && ((get_flag(55,6) == 0) || (get_terrain(25,26) == 378))) {
		//light the fires
		set_terrain(25,26,379);
		set_terrain(27,26,379);
		set_flag(55,6,1);
		}
	if((get_current_tick() % 5000 < 3750) && ((get_flag(55,6) == 1) || (get_terrain(25,26) == 379))) {
		//put out the fires
		set_terrain(25,26,378);
		set_terrain(27,26,378);
		force_instant_terrain_redraw();
		set_flag(55,6,0);
		}
	
	//get rid of the ships
	if((get_flag(55,0) == 1) && (get_terrain(35,40) == 491)) {
		set_terrain(35,40,0);
		set_terrain(36,40,0);
		set_terrain(39,16,0);
		set_terrain(39,47,0);
		set_terrain(25,9,0);
		set_terrain(26,9,0);
		set_terrain(11,24,0);
		set_terrain(12,24,0);
		set_terrain(24,42,0);
		set_terrain(24,43,0);
		eliminate_outdoor_enc(55,3); //nuke Loyalists
		place_out_spec_enc(0,27,29); //create the guards
		}
		
break;

beginstate 10; //don't charge at the ships
	if(get_flag(55,0) != 0)
		end();
	message_dialog("Charging directly at the Loyalist ships, while perhaps brave in some circles, is probably suicidal.","You turn back.");
	block_entry(1);
break;

beginstate 15; //sand runes
	if(get_flag(55,8) == 1)
		end();
	if(has_item(445) == 0)
		end();
	
	reset_dialog();
	add_dialog_str(0,"The key you got from the crazy hermit starts vibrating in your pack, and you feel disoriented for a moment. The feeling builds, culminating in an awful headache.",0);
	add_dialog_str(1,"The pain is unpleasant enough that you almost don't notice when a large demon appears a short distance away on the sand.",0);
	run_dialog(1);
	
	create_out_spec_enc(1);
	
	set_flag(55,8,1);
break;

beginstate 16; //demon fight
	set_out_fight_town_loaded(13);
break;

beginstate 20; //meeting the Loyalist patrol
	//armor check
	safety = 0;
	if((get_flag(55,1) == 0) && (safety == 0)) {
		pc_counter = 0;
		//armor check
		while((pc_counter < 4) && (safety == 0)) {
			if((char_ok(pc_counter) == 1) && (item_type_in_slot(pc_counter,0) != 447))
				safety = 1; //not safe if armor isn't equipped
			pc_counter = pc_counter + 1;
			}
		}

	//Results
	if(get_flag(55,3) == 1) {
		reset_dialog();
		add_dialog_str(0,"You approach the patrolling Loyalists, who immediately recognize you and charge.",0);
		add_dialog_str(1,"Well, what did you expect?",0);
		run_dialog(1);
		}
	else if((get_flag(55,1) == 0) && (safety == 1)) {
		reset_dialog();
		if(get_flag(55,2) == 0)
			add_dialog_str(0,"You stumble upon a rather large patrol of Darkside Loyalists. They immediately spot you, and then charge.",0);
		else
			add_dialog_str(0,"As you approach the patrol, you realize that you forgot your armor. The Loyalists notice, and immediately charge.",0);
		run_dialog(1);
		set_flag(55,1,1);
		set_flag(55,2,1);
		}
	else if((get_flag(55,1) == 1) && (safety == 0)) {
		message_dialog("As soon as the Loyalists see you, they recognize you and charge. Looks like the armor isn't fooling them anymore.","");
		}
	else if((safety == 0) && (get_flag(55,2) == 0)) {
		reset_dialog();
		add_dialog_str(0,"You stumble upon a rather large group of Darkside Loyalists. For a moment, you worry that you'll be overwhelmed by sheer force of numbers, but then they see your armor.",0);
		add_dialog_str(1,"Amazingly, the armor you stole from their slain comrades is enough to convince them you're on the same side. You mutter a few pleasantries and slip past them.",0);
		run_dialog(1);
		set_flag(55,2,1);
		outdoor_enc_result(2);
		}
	else if((safety == 0) && (get_flag(55,2) == 1)) {
		print_str("You manage to squeak past the patrol again.");
		outdoor_enc_result(2);
		}
break;

beginstate 21; //defeating the Loyalist patrol

	set_flag(55,3,1);
break;

beginstate 22; //fleeing from the Loyalist patrol

	set_flag(55,4,1);
break;

beginstate 25; //approaching the dock
	if(get_flag(55,5) == 0) {
		reset_dialog();
		if(in_boat() != -1)
			add_dialog_str(0,"You paddle up to the docks of Copperpeak, which have been rather thoroughly trashed.",0);
		else
			add_dialog_str(0,"You creep along the beach to the docks of Copperpeak, which have been rather thoroughly trashed.",0);
		add_dialog_str(1,"At first, it looks like the docks are deserted. But then, a campfire comes into view, with a few figures seated around it. More than likely, they are Loyalists.",0);
		add_dialog_str(2,"There might also be a few more figures skulking about the ruins, but it's hard to see from here. You'll have to get past them somehow.",0);
		run_dialog(1);
		
		//check to see if armor is equipped
		pc_counter = 0;
		while((pc_counter < 4) && (safety == 0)) {
			if((char_ok(pc_counter) == 1) && (item_type_in_slot(pc_counter,0) != 447))
				safety = 1; //not safe if armor isn't equipped
			pc_counter = pc_counter + 1;
			}
		if(safety == 0) {
			reset_dialog();
			add_dialog_str(0,"A thought occurs to you. You're already wearing the Darkside Loyalists' armor. Maybe if you just kept quiet and snuck past the soldiers, you won't have any trouble.",0);
			add_dialog_str(1,"Or, you could just try to fight them all. Either way.",0);
			run_dialog(1);
			}
		else if(has_num_of_item(447) >= party_size()) {
			reset_dialog();
			add_dialog_str(0,"A thought occurs to you. You have a few sets of the Loyalists' armor. Enough to disguise yourselves, anyway.",0);
			add_dialog_str(1,"Maybe if you just kept quiet and snuck past the soldiers, you won't have any trouble. Or, you could just try to fight them all. Either way.",0);
			run_dialog(1);
			}
		set_flag(55,5,1);
		}
break;

beginstate 30; //meeting the guards, blocking the way back into Copperpeak
	outdoor_enc_result(1); //don't fight
	if(get_flag(55,7) == 1)
		print_str("You exchange friendly greetings with the guards.");
	else {
		reset_dialog();
		add_dialog_str(0,"You encounter a group of Empire soldiers, some of whom look familiar. You realize that they are members of the Copperpeak garrison.",0);
		add_dialog_str(1,"After a tense moment, they decide you're not Loyalists, and they approach you. They explain that the city is closed off until the Empire makes a full investigation.",0);
		add_dialog_str(2,"Even though you're responsible for their freedom, you're not being allowed in any time soon. The guards apologize and let you get on your way.",0);
		run_dialog(1);
		set_flag(55,7,1);
		}
break;

beginstate 40; //don't let you past the guards after the Loyalists are gone
	if(get_flag(8,0) == 1)
		block_entry(1);
break;